home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Lose Your Marbles! 1.0 / source / ls code ƒ / ls endgame.c next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  6.5 KB  |  253 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        ls endgame.c
  4.  
  5. Purpose:    This module handles drawing the endgame board.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program in a file named "GNU General Public License".
  19. If not, write to the Free Software Foundation, 675 Mass Ave,
  20. Cambridge, MA 02139, USA.
  21.  
  22. \**********************************************************************/
  23.  
  24. #include "ls endgame.h"
  25. #include "ls find.h"
  26. #include "ls meat.h"
  27. #include "sounds.h"
  28. #include "graphics.h"
  29. #include "menus.h"
  30. #include "util.h"
  31. #include "V bars scroll thin fade.h"
  32. #include "program globals.h"
  33.  
  34. static    Boolean                gShowingEndGame;
  35. static    PicHandle            gCongratsColorPict, gCongratsBWPict;
  36. static    PicHandle            gWinnerColorPict, gWinnerBWPict;
  37.  
  38. enum
  39. {
  40.     congratsColorID=400,
  41.     congratsBWID,
  42.     winnerColorID,
  43.     winnerBWID
  44. };
  45.  
  46. void DrawCenteredString(Str255 theStr, short *theY, short width);
  47. void AddToString(Str255 theStr, Str255 addStr);
  48. short BestSolution(short numRows, short numColumns);
  49. short FindNumUsed(void);
  50.  
  51. Boolean GameOverQQ(void)
  52. {
  53.     short            theRow, theColumn;
  54.     
  55.     for (theRow=0; theRow<gNumRows; theRow++)
  56.     {
  57.         for (theColumn=0; theColumn<gNumColumns; theColumn++)
  58.         {
  59.             if (Board[theRow][theColumn]==0)
  60.                 return FALSE;
  61.         }
  62.     }
  63.     
  64.     return !FindProblem(&theRow, &theColumn, &theRow, &theColumn);
  65. }
  66.  
  67. void CheckEndGame(WindowDataHandle theData)
  68. {
  69.     if (GameOverQQ())
  70.     {
  71.         VerticalBarsScrollThinFade(GetWindowGrafPtr(theData)->portRect, &black);
  72.         gShowingEndGame=TRUE;
  73.         AdjustMenus();
  74.         DrawMenuBar();
  75.         DoSound(sound_endgame, TRUE);
  76.         (**theData).offscreenNeedsUpdate=TRUE;
  77.         UpdateTheWindow((ExtendedWindowDataHandle)theData);
  78.     }
  79. }
  80.  
  81. Boolean ShowingEndGameQQ(void)
  82. {
  83.     return gShowingEndGame;
  84. }
  85.  
  86. void DontShowEndGame(WindowDataHandle theData)
  87. {
  88.     gShowingEndGame=FALSE;
  89.     AdjustMenus();
  90.     DrawMenuBar();
  91.     (**theData).offscreenNeedsUpdate=TRUE;
  92.     UpdateTheWindow((ExtendedWindowDataHandle)theData);
  93. }
  94.  
  95. void InitTheEndGame(void)
  96. {
  97.     gCongratsColorPict=gCongratsBWPict=gWinnerColorPict=gWinnerBWPict=0L;
  98.     gShowingEndGame=FALSE;
  99. }
  100.  
  101. void ShutDownTheEndGame(void)
  102. {
  103.     gCongratsColorPict=ReleaseThePict(gCongratsColorPict);
  104.     gCongratsBWPict=ReleaseThePict(gCongratsBWPict);
  105.     gWinnerColorPict=ReleaseThePict(gWinnerColorPict);
  106.     gWinnerBWPict=ReleaseThePict(gWinnerBWPict);
  107. }
  108.  
  109. void DrawEndGame(WindowDataHandle theData, short theDepth)
  110. {
  111.     GrafPtr            curPort;
  112.     short            theX, theY;
  113.     Boolean            isColor;
  114.     short            best;
  115.     short            numUsed;
  116.     short            width;
  117.     Str255            theStr, numStr;
  118.     
  119.     isColor=(theDepth>2);
  120.     GetPort(&curPort);
  121.     FillRect(&(curPort->portRect), black);
  122.     width=curPort->portRect.right-curPort->portRect.left;
  123.     theX=(**theData).windowWidth/2;
  124.     theY=(curPort->portRect.bottom-curPort->portRect.top)/3;
  125.     best=BestSolution(gNumRows, gNumColumns);
  126.     numUsed=FindNumUsed();
  127.     if (numUsed==best)
  128.     {
  129.         if (isColor)
  130.             gWinnerColorPict=DrawThePicture(gWinnerColorPict, winnerColorID, theX, theY, TRUE);
  131.         else
  132.             gWinnerBWPict=DrawThePicture(gWinnerBWPict, winnerBWID, theX, theY, TRUE);
  133.         
  134.         theStr[0]=0x00;
  135.         AddToString(theStr, "\pYou’ve completed the ");
  136.         NumToString(gNumRows, numStr);
  137.         AddToString(theStr, numStr);
  138.         AddToString(theStr, "\p x ");
  139.         NumToString(gNumColumns, numStr);
  140.         AddToString(theStr, numStr);
  141.         AddToString(theStr, "\p board");
  142.         theY=(curPort->portRect.bottom-curPort->portRect.top)/2;
  143.         DrawCenteredString(theStr, &theY, width);
  144.         theStr[0]=0x00;
  145.         AddToString(theStr, "\pwith the minimum number of pieces!");
  146.         DrawCenteredString(theStr, &theY, width);
  147.         theStr[0]=0x00;
  148.         DrawCenteredString(theStr, &theY, width);
  149.         if (gNumRows<MAX_ROWS)
  150.         {
  151.             AddToString(theStr, "\p(Time to try the ");
  152.             NumToString(gNumRows+1, numStr);
  153.             AddToString(theStr, numStr);
  154.             AddToString(theStr, "\p x ");
  155.             NumToString(gNumColumns+1, numStr);
  156.             AddToString(theStr, numStr);
  157.             AddToString(theStr, "\p board, no?)");
  158.             DrawCenteredString(theStr, &theY, width);
  159.         }
  160.         else
  161.         {
  162.             AddToString(theStr, "\p(If you’ve already completed the other");
  163.             DrawCenteredString(theStr, &theY, width);
  164.             theStr[0]=0x00;
  165.             AddToString(theStr, "\plevels, you’re better than the author!)");
  166.             DrawCenteredString(theStr, &theY, width);
  167.         }
  168.     }
  169.     else
  170.     {
  171.         if (isColor)
  172.             gCongratsColorPict=DrawThePicture(gCongratsColorPict, congratsColorID, theX, theY, TRUE);
  173.         else
  174.             gCongratsBWPict=DrawThePicture(gCongratsBWPict, congratsBWID, theX, theY, TRUE);
  175.         
  176.         theStr[0]=0x00;
  177.         AddToString(theStr, "\pYou’ve completed the ");
  178.         NumToString(gNumRows, numStr);
  179.         AddToString(theStr, numStr);
  180.         AddToString(theStr, "\p x ");
  181.         NumToString(gNumColumns, numStr);
  182.         AddToString(theStr, numStr);
  183.         AddToString(theStr, "\p board");
  184.         theY=(curPort->portRect.bottom-curPort->portRect.top)/2;
  185.         DrawCenteredString(theStr, &theY, width);
  186.         theStr[0]=0x00;
  187.         AddToString(theStr, "\pwith ");
  188.         NumToString(numUsed, numStr);
  189.         AddToString(theStr, numStr);
  190.         AddToString(theStr, "\p unique pieces.");
  191.         DrawCenteredString(theStr, &theY, width);
  192.         theStr[0]=0x00;
  193.         DrawCenteredString(theStr, &theY, width);
  194.         AddToString(theStr, "\p(But can you do it with only ");
  195.         NumToString(best, numStr);
  196.         AddToString(theStr, numStr);
  197.         AddToString(theStr, "\p?)");
  198.         DrawCenteredString(theStr, &theY, width);
  199.     }
  200. }
  201.  
  202. short BestSolution(short numRows, short numColumns)
  203. {
  204.     switch (numRows)
  205.     {
  206.         case 5:    return 5;
  207.         case 6:    return 7;
  208.         case 7:    return 7;
  209.         case 8:    return 9;
  210.     }
  211. }
  212.  
  213. short FindNumUsed(void)
  214. {
  215.     short            theRow, theColumn;
  216.     Boolean            used[11];
  217.     short            numUsed;
  218.     
  219.     for (theRow=0; theRow<11; theRow++)
  220.         used[theRow]=FALSE;
  221.     
  222.     for (theRow=0; theRow<gNumRows; theRow++)
  223.     {
  224.         for (theColumn=0; theColumn<gNumColumns; theColumn++)
  225.         {
  226.             used[Board[theRow][theColumn]]=TRUE;
  227.         }
  228.     }
  229.     
  230.     numUsed=0;
  231.     for (theRow=0; theRow<11; theRow++)
  232.         if (used[theRow])
  233.             numUsed++;
  234.     
  235.     return numUsed;
  236. }
  237.  
  238. void DrawCenteredString(Str255 theStr, short *theY, short width)
  239. {
  240.     TextFont(geneva);
  241.     TextSize(9);
  242.     TextMode(srcXor);
  243.     MoveTo((width-StringWidth(theStr))/2, *theY);
  244.     DrawString(theStr);
  245.     (*theY)+=12;
  246. }
  247.  
  248. void AddToString(Str255 theStr, Str255 addStr)
  249. {
  250.     Mymemcpy((Ptr)&theStr[theStr[0]+1], (Ptr)&addStr[1], addStr[0]);
  251.     theStr[0]+=addStr[0];
  252. }
  253.